package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.mgrsite.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategyCommend")
public class StrategyCommendController {
    @Value("${file.dir}")
    private String fileDir;
    @Reference
    private IStrategyCommendService strategyCommendService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model){
        PageInfo pageInfo = strategyCommendService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "strategyCommend/list";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(StrategyCommend strategyCommend, MultipartFile pic){
        AjaxResult result = new AjaxResult();
        //保存图片
        String coverUrl = UploadUtil.upload(pic, fileDir);
        //设置路径
        strategyCommend.setCoverUrl("/" + coverUrl);
        try {
            strategyCommendService.save(strategyCommend);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(StrategyCommend strategyCommend, MultipartFile pic){
        AjaxResult result = new AjaxResult();
        //保存图片
        String coverUrl = UploadUtil.upload(pic, fileDir);
        //设置路径
        strategyCommend.setCoverUrl("/" + coverUrl);
        try {
            strategyCommendService.update(strategyCommend);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

}
